// pages/personal/personaldata/personaldata.js
import { decryptGetPhone, changeGender } from '../../../../config/config'
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    genderArray: ['男', '女'],
    loginMsg: {},
    phoneNumber: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      let option = JSON.parse(options.userInfo)
      this.setData({
        userInfo: option,
        userInfoGender: option.gender,
        phoneNumber: option.phone
      })
    }
  },
  backeIndex() {
    wx.navigateBack({
      delta: 1
    })
  },
  deliveryAddress() {
    wx.navigateTo({
      url: '../deliveryAddress/deliveryAddress'
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
    this.modifyGender(e.detail.value)
  },
  modifyGender(e) {
    let gender
    if (e == 0) {
      gender = 'male'
    } else {
      gender = 'female'
    }
    let option = {}
    option.gender = gender
    option.icon = ''
    option.nickname = ''
    option.phone = this.data.phoneNumber
    app.reqFun.requestJson('POST',changeGender,option,(res)=>{
      if(res.data.success==1){
        console.log('gender', gender)
        this.setData({
          userInfoGender: gender
        })
      }
    }) 
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let loginMsg = wx.getStorageSync('loginMsg')   
    this.setData({
      loginMsg: loginMsg
    })
  },
  getPhoneNumber: function (e) {
    let loginMsg = this.data.loginMsg
    if (e.detail.iv && e.detail.encryptedData) {
      let option = {}
      option.encryptedData = e.detail.encryptedData
      option.iv = e.detail.iv
      option.openid = loginMsg.openid
      option.session_key = loginMsg.session_key
      app.reqFun.requestJson('POST',decryptGetPhone,option,(res)=>{
        if(res.data.success==1){
          console.log('data', res.data.data)
          this.setData({
            phoneNumber: res.data.data
          })
        }
      }) 
    } else {
      console.log('取消')
    }
  },
  //刷新微信信息
  refreshWxInfo: function() {
    let that = this
    if (wx.getUserProfile) {
      app.getUserProfile(function(){
        let loginMsg = wx.getStorageSync('loginMsg')
        let userInfo = loginMsg.userInfo
        that.setData({
          loginMsg: loginMsg,
          userInfo: userInfo,
          userInfoGender: userInfo.gender,
          phoneNumber: userInfo.phone
        })
      })
    } else {
      wx.showToast({
        title: '您的微信版本太低了',
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})